﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Common;
using SYH.Config;
using System.Data;

namespace SYH.Public.DAL
{
    public class Public_MessageCount
    {
        #region 根据userid和count修改已读未读数
        /// <summary>
        /// 根据userid和count修改已读未读数
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public int UpdateMsgCount(int userid, int count)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Public_MessageCount set ReadCount = ReadCount + @Count, NoReadCount = NoReadCount - @Count where userId = @UserId");
            SqlParameter[] parameters ={
                    new SqlParameter("@UserId",SqlDbType.Int,4),
                    new SqlParameter("@Count",SqlDbType.Int,4)
                                       };
            parameters[0].Value = userid;
            parameters[1].Value = count;
            object obj = SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            return Common.TypeParse.ObjectToInt(obj, -1);
        }
        #endregion

        #region 根据userid读收件箱中未读消息数
        /// <summary>
        /// 收件箱中未读消息数
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public int GetNoReadReceivedMessage(int userId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 NoReadCount from Public_MessageCount where userId = @UserId");
            SqlParameter[] parameters = {
				new SqlParameter("@UserId", SqlDbType.Int,4)
            };
            parameters[0].Value = userId;
            return Common.TypeParse.ObjectToInt(Common.SqlHelper.ExecuteScalar(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters), 0);
        }
        #endregion

        #region 根据消息类型获取未读消息数
        /// <summary>
        /// 根据消息类型获取未读消息数
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public int GetNoReadCountByType(int UserId, string type)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select COUNT (1) from dbo.Public_MessageReceiver where ReceiverId=@UserId and IsDeleted=0 and IsReader=0 and TypeId in(" + type + ") ");
            SqlParameter[] parameters = {
				new SqlParameter("@UserId", SqlDbType.Int,4)
            };
            parameters[0].Value = UserId;
            return Common.TypeParse.ObjectToInt(Common.SqlHelper.ExecuteScalar(WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters), 0);
        }
        #endregion
    }
}
